package co.fitstart.entity.site;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;
import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.course.schedule.CourseSchedule;

/**
 * 
 * @author mohanlan
 * 
 */
@Entity
@Table(name="site_schedule_record")
@DynamicUpdate
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class RoomScheduleRecord {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 所属门店 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Site site;
    
    /** 占用房间类型 */
    @Enumerated(EnumType.ORDINAL)
    private RoomType roomType;
    
    /** 课程的code */
    private String roundCode;
    
    /** 上课日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @org.hibernate.annotations.Type(type = "date")
    private Date occurDate;
    
    /** 上课时间 */
    private String occurHour;
    
    /** 占用空间份额 */
    private int amount;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public RoomScheduleRecord() {
    }
    
    /**
     * Create new group site schedule record
     * 
     * @param site
     * @param roundCode
     * @param occurDate
     * @param occurHour
     * @param roomType
     * @param amount
     */
    public RoomScheduleRecord(Site site, String roundCode, Date occurDate, String occurHour, RoomType roomType, int amount) {
        this.site = site;
        this.roundCode = roundCode;
        this.occurDate = occurDate;
        this.occurHour = occurHour;
        this.amount = amount;
        this.roomType = roomType;
        
        this.disabled = false;
    }
    
    /**
     * Update site schedule record time
     * 
     * @param day
     * @param hour
     * @return
     */
    public RoomScheduleRecord updateTime(Date day, String hour, Site site) {
        this.occurDate = day;
        this.occurHour = hour;
        this.site = site;
        return this;
    }

    /**
     * 
     * @param day
     * @param hour
     * @param site
     * @param roomType
     * @return
     */
    public RoomScheduleRecord updateTime(Date day, String hour, Site site, RoomType roomType) {
        this.occurDate = day;
        this.occurHour = hour;
        this.site = site;
        this.roomType = roomType;
        return this;
    }
    
    /**
     * 
     * @param courseSchedule
     * @return
     */
    public RoomScheduleRecord update(CourseSchedule courseSchedule) {
        this.site = courseSchedule.getSite();
        this.roomType = courseSchedule.getRoomType();
        this.roundCode = courseSchedule.getCourseRound().getCode();
        this.occurDate = courseSchedule.getOccurDate();
        this.occurHour = courseSchedule.getOccurHour();
        return this;
    }
    
    /**
     * Disable 
     * 
     * @return
     */
    public RoomScheduleRecord disabled() {
        this.disabled = true;
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getDayhour() {
        
        if(this.occurDate != null && StringUtils.isNotBlank(occurHour)) {
            return DateFormatUtils.format(this.occurDate) + "|" + this.occurHour; 
        }
        
        return null;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @return the type
     */
    public RoomType getType() {
        return roomType;
    }

    /**
     * @return the roundCode
     */
    public String getRoundCode() {
        return roundCode;
    }

    /**
     * @return the occurDate
     */
    public Date getOccurDate() {
        return occurDate;
    }

    /**
     * @return the occurHour
     */
    public String getOccurHour() {
        return occurHour;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @param roomType the type to set
     */
    public void setType(RoomType roomType) {
        this.roomType = roomType;
    }

    /**
     * @param roundCode the roundCode to set
     */
    public void setRoundCode(String roundCode) {
        this.roundCode = roundCode;
    }

    /**
     * @param occurDate the occurDate to set
     */
    public void setOccurDate(Date occurDate) {
        this.occurDate = occurDate;
    }

    /**
     * @param occurHour the occurHour to set
     */
    public void setOccurHour(String occurHour) {
        this.occurHour = occurHour;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
